// pages/chuangjian/chuangjian.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navH:'',
    navTop:'',
    pageName:"创建名片",
    showNav:true,

    img1:'',
    img2:'',
    img3:'',


    jiaitems:false,
    jiaitemg:false,
    type:'',

    name:"",
    phone1:'',
    phone2:'',
    zhiwu:'',
    weixin:'',
    emil:'',
    guhua1:'',
    guhua2:'',
    wangzhi:'',
    address:'北京市中关村昌平科技园区火炬街23号',

    famou_id:"",
    img1lao:""
  },

  baocun(){
    // http:// api/index/famous_add
    var that=this
    var phone,guhua

    if(that.data.img1==''){
      wx.showToast({
        title: '头像不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(that.data.name==''){
      wx.showToast({
        title: '姓名不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(that.data.phone1==''){
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(that.data.jiaitems){
      if(that.data.phone2==''){

        wx.showToast({
          title: '手机号2不能为空',
          icon: 'none',
          duration: 2000
        })
        return
      }else{
        phone=that.data.phone1 + '/' + that.data.phone2
      }
    }else{
      phone=that.data.phone1
    }


    if(that.data.zhiwu==''){
      wx.showToast({
        title: '职务不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }

    if(that.data.weixin==''){
      wx.showToast({
        title: '微信不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(that.data.emil==''){
      wx.showToast({
        title: '邮箱不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(that.data.guhua1==''){
      wx.showToast({
        title: '固定电话不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }

    if(that.data.jiaitemg){
      if(that.data.guhua2==''){
        wx.showToast({
          title: '固定电话不能为空',
          icon: 'none',
          duration: 2000
        })
        return
      }else{
        guhua=that.data.guhua1 + '/' + that.data.guhua2
      }
    }else{
      guhua=that.data.guhua1
    }

    if(that.data.wangzhi==''){
      wx.showToast({
        title: '网址不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(that.data.address==''){
      wx.showToast({
        title: '公司地址不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }

    if(that.data.type=='add'){
      wx.uploadFile({
        url: src + '/api/index/famous_add',
        filePath: that.data.img1,
        name: 'avatar',
        header: { "Content-Type": "multipart/form-data" },
        formData: {
          uqid: wx.getStorageSync('uqid'),
          name: that.data.name,
          phone:phone,
          position:that.data.zhiwu,    
          emails:that.data.emil,
          fixed_telephone:guhua,
          personal_company_url:that.data.wangzhi,
          personal_company_address:that.data.address,
          wechat_number:that.data.weixin,
        },
        success(res) {
         
          // console.log(JSON.parse(res.data))
  
          var res=JSON.parse(res.data)
          console.log(res)
          if (res.error == 0) {
          
            wx.navigateBack()
    
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000
            })
    
          }
        }
      })
    }

    if(that.data.type=='edit'){

      if(that.data.img1==that.data.img1lao){
        ajax('/api/index/famous_edit', {
          data: {
            avatar:'',
            uqid: wx.getStorageSync('uqid'),
            famou_id:that.data.famou_id,
            name: that.data.name,
            phone:phone,
            position:that.data.zhiwu,    
            emails:that.data.emil,
            fixed_telephone:guhua,
            personal_company_url:that.data.wangzhi,
            personal_company_address:that.data.address,
            wechat_number:that.data.weixin,
          },
          header: {
            'Accept': 'application/json', // 默认值
          },
          method: 'post',
        }, res => {
          console.log(res)
          if (res.error == 0) {
            
            wx.navigateBack()
    
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000
            })
    
          }
        })
      }else{
        wx.uploadFile({
          url: src + '/api/index/famous_edit',
          filePath: that.data.img1,
          name: 'avatar',
          header: { "Content-Type": "multipart/form-data" },
          formData: {
            uqid: wx.getStorageSync('uqid'),
            famou_id:that.data.famou_id,
            name: that.data.name,
            phone:phone,
            position:that.data.zhiwu,    
            emails:that.data.emil,
            fixed_telephone:guhua,
            personal_company_url:that.data.wangzhi,
            personal_company_address:that.data.address,
            wechat_number:that.data.weixin,
          },
          success(res) {
           
            // console.log(JSON.parse(res.data))
    
            var res=JSON.parse(res.data)
            console.log(res)
            if (res.error == 0) {
            
              wx.navigateBack()
      
            } else {
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
              })
      
            }
          }
        })
      }
      console.log('11111')
      
    }

    


 
  },

  navBack(){
    wx.navigateBack()
  },

  getfuzhixinxi(){
    var that = this
    ajax('/api/index/company_info', {
      data: {
        uqid: wx.getStorageSync('uqid'),
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'get'
    }, res => {
      console.log(res)
      if (res.error == 0) {
        console.log(res)
        var guhualist=res.data.phone.split('/')
        if(guhualist[1]){
          this.setData({
            jiaitemg:true,
            guhua1:guhualist[0],
            guhua2:guhualist[1],
          })
        }else{
          this.setData({
            jiaitemg:false,
            guhua1:guhualist[0],
            guhua2:'',
          })
        }
        that.setData({
          gongsiming:res.data.name,
          address:res.data.address,
          wangzhi:res.data.url,
          
        })
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 2000
        })

      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var type=options.type
    var famou_id=options.id
    this.setData({
      type:type,
      famou_id:famou_id
    })

    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#2A2F42',
    })
    
    this.setData({
      navH:wx.getStorageSync('navHeight'),
      navTop:wx.getStorageSync('navTop'),
    })
    

    if(type=='edit'){
      console.log('edit')
      this.getall()
    }else(
      this.getfuzhixinxi()
    )
  },

  getall(){
    var that = this;
    ajax('/api/index/famous', {
      data: {
        uqid:wx.getStorageSync('uqid')
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
    }, res => {
      console.log(res)
      if (res.error == 0) {
        var phonelistt=res.data.info.phone.split('/')
        console.log(phonelistt)
        var guhualist=res.data.info.fixed_telephone.split('/')
        if(phonelistt[1]){
          this.setData({
            jiaitems:true,
            phone1:phonelistt[0],
            phone2:phonelistt[1],
          })
        }else{
          this.setData({
            jiaitems:false,
            phone1:phonelistt[0],
            phone2:'',
          })
        }

        if(guhualist[1]){
          this.setData({
            jiaitemg:true,
            guhua1:guhualist[0],
            guhua2:guhualist[1],
          })
        }else{
          this.setData({
            jiaitemg:false,
            guhua1:guhualist[0],
            guhua2:'',
          })
        }

        this.setData({
            img1:res.data.info.avatar,
            img1lao:res.data.info.avatar,
            name: res.data.info.name,
           
            zhiwu:res.data.info.position,    
            emil:res.data.info.emails,
            wangzhi:res.data.info.personal_company_url,
            address:res.data.info.personal_company_address,
            weixin:res.data.info.wechat_number,
          
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })

      }
    })
  },

  shoujiitem(e){
    var type=e.currentTarget.dataset.type
    if(type=='1'){
      this.setData({
        jiaitems:false
      })
    }else{
      this.setData({
        jiaitems:true
      })
    }
  },

  gudingitem(e){
    var type=e.currentTarget.dataset.type
    if(type=='1'){
      this.setData({
        jiaitemg:false
      })
    }else{
      this.setData({
        jiaitemg:true
      })
    }
  },



  upimg: function (e) {

    var type=e.currentTarget.dataset.type;
    var that = this;
    var uqid = this.data.uqid;
    wx.showActionSheet({
      itemList: ['从相册选取', '拍照'],
      success(res) {
        var index = res.tapIndex;
        if(index == 0){
          wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album'],
            success(res) {
              // tempFilePath可以作为img标签的src属性显示图片
              const tempFilePaths = res.tempFilePaths;
      
              that.setData({
                img1:tempFilePaths[0]
              })
            }
          })
        }else if(index == 1){
          wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['camera'],
            success(res) {
              // tempFilePath可以作为img标签的src属性显示图片
              const tempFilePaths = res.tempFilePaths;
              that.setData({
                img1:tempFilePaths[0]
              })
            }
          })
        }

      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
    
  },

  shuruname(e){
    var name=e.detail.value
    // console.log(e)
    this.setData({
      name:name
    })
  },
  shuruphone2(e){
    var phone2=e.detail.value
    // console.log(e)
    this.setData({
      phone2:phone2
    })
  },
  shuruphone1(e){
    var phone1=e.detail.value
    // console.log(e)
    this.setData({
      phone1:phone1
    })
  },
  shuruzhiwu(e){
    var zhiwu=e.detail.value
    // console.log(e)
    this.setData({
      zhiwu:zhiwu
    })
  },
  shuruweixin(e){
    var weixin=e.detail.value
    // console.log(e)
    this.setData({
      weixin:weixin
    })
  },
  shuruemil(e){
    var emil=e.detail.value
    // console.log(e)
    this.setData({
      emil:emil
    })
  },
  shuruguhua1(e){
    var guhua1=e.detail.value
    // console.log(e)
    this.setData({
      guhua1:guhua1
    })
  },
  shuruguhua2(e){
    var guhua2=e.detail.value
    // console.log(e)
    this.setData({
      guhua2:guhua2
    })
  },

  shuruwangzhi(e){
    var wangzhi=e.detail.value
    // console.log(e)
    this.setData({
      wangzhi:wangzhi
    })
  },

  shuruaddress(e){
    var address=e.detail.value
    // console.log(e)
    this.setData({
      address:address
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})